// import 'core-js/stable'
// import 'regenerator-runtime/runtime'
import 'babel-polyfill'
import Vue from 'vue'

import Cookies from 'js-cookie'

import Element from 'element-ui'
import './assets/styles/element-variables.scss'
import scroll from 'vue-seamless-scroll'
Vue.use(scroll)
import 'lib-flexible/flexible.js'
// import VueJsonp from 'vue-jsonp'
// Vue.use(VueJsonp)
// 默认注册的组件名是 vue-seamless-scroll
Vue.use(scroll,{componentName: 'scroll-seamless'})
import VueChatScroll from 'vue-chat-scroll'
Vue.use(VueChatScroll)
import '@/assets/styles/index.scss' // global css
import '@/assets/styles/hmp.scss'
import App from './App'
import store from './store'
import router from './router'
import directive from './directive' // directive
import plugins from './plugins' // plugins
import moment from 'moment'
import {
  download,
  downloads,
  downloadsList
} from '@/utils/request'

import './assets/icons' // icon
import './permission' // permission control
import {
  getDicts
} from "@/api/system/dict/data";
import {
  getConfigKey
} from "@/api/system/config";
import {
  parseTime,
  resetForm,
  addDateRange,
  selectDictLabel,
  selectDictLabels,
  selectDictLabelOld,
  handleTree,
  addDateRangeSearch
} from "@/utils/hmp";
// 分页组件
import Pagination from "@/components/Pagination";
// 自定义表格工具组件
import RightToolbar from "@/components/RightToolbar"
// 富文本组件
import Editor from "@/components/Editor"
// 文件上传组件
import FileUpload from "@/components/FileUpload"
// 图片上传组件
import ImageUpload from "@/components/ImageUpload"
// 图片预览组件
import ImagePreview from "@/components/ImagePreview"
// 字典标签组件
import DictTag from '@/components/DictTag'
// 头部标签组件
import VueMeta from 'vue-meta'
// 字典数据组件
import DictData from '@/components/DictData'
import Fragment from 'vue-fragment'
Vue.use(Fragment.Plugin)

// 全局方法挂载
Vue.prototype.getDicts = getDicts
Vue.prototype.getConfigKey = getConfigKey
Vue.prototype.parseTime = parseTime
Vue.prototype.resetForm = resetForm
Vue.prototype.addDateRange = addDateRange
Vue.prototype.addDateRangeSearch = addDateRangeSearch
Vue.prototype.selectDictLabel = selectDictLabel
Vue.prototype.selectDictLabels = selectDictLabels
Vue.prototype.selectDictLabelOld = selectDictLabelOld
Vue.prototype.download = download
Vue.prototype.downloads = downloads
Vue.prototype.downloadsList = downloadsList

Vue.prototype.handleTree = handleTree

// 全局组件挂载
Vue.component('DictTag', DictTag)
Vue.component('Pagination', Pagination)
Vue.component('RightToolbar', RightToolbar)
Vue.component('Editor', Editor)
Vue.component('FileUpload', FileUpload)
Vue.component('ImageUpload', ImageUpload)
Vue.component('ImagePreview', ImagePreview)

// 日期处理
Vue.prototype.$moment = moment

moment.locale(`zh-cn`)

// 定义全局时间格式过滤器
Vue.filter(`dateFormat`, function (dataStr, pattern = `YYYY-MM-DD HH:mm:ss`) {
  return moment(dataStr).format(pattern);
})


//省下拉框change事件
Vue.prototype.provinceChange = function (val, that, objName) {
  let objNameVal = objName ? objName : 'queryParams';
  that[objNameVal].cityCode = undefined;
  that[objNameVal].countyCode = undefined;
  getCity(val, objNameVal, that);
}
Vue.prototype.tostrings = function (val) {
  // var s = val + "";
  var s = val.toString()
  return s
}
//市下拉框change事件
Vue.prototype.cityChange = function (val, that, objName) {
  let objNameVal = objName ? objName : 'queryParams';
  that[objNameVal].countyCode = undefined;
  if (val) {
    getCounty(val).then((res) => {
      if (objNameVal == "queryParams") {
        that.countyList = res.data;
      } else {
        that.countyListForm = res.data;
      }
    });
  } else {
    if (objNameVal == "queryParams") {
      that.countyList = [];
    } else {
      that.countyListForm = [];
    }
  }
}

//打开天眼查页面
Vue.prototype.openTYC = function (companyName) {
  if (companyName) {
    window.open("https://www.tianyancha.com/search?key=" + companyName);
  } else {
    this.$message({
      showClose: true,
      message: "请输入查询内容",
      type: "warn"
    });
  }
}

//导入时下载示例
Vue.prototype.downloadSample = function () {
  window.location.href = "http://www.wik6.cn/import.doc";
}
// 关键字高亮显示
function hightLight(el, binding) {
  const match = binding.value;
  let keywordArr = match.split("");
  // const reg = new RegExp(match, "g");
  let txt = binding.arg + "";
  let str = "";
  if (txt) {
    // str = txt.replace(reg, `<span style="color:#ed0606">${match}</span>`);
    keywordArr.forEach(item => {
      if (txt.indexOf(item) !== -1 && item !== "") {
        txt = txt.replace(
          new RegExp(item + "{1,1}"),
          // new RegExp(item, "g"),
          `<span style="color:#ed0606">${item}</span>`
        );
      }
    });
    str = txt;
  } else {
    str = "";
  }
  el.innerHTML = str;
}
// 关键字高亮显示--vue指令
Vue.directive("hightlight", {
  bind(el, binding) {
    hightLight(el, binding);
  },
  componentUpdated(el, binding) {
    hightLight(el, binding);
  },
});

Vue.prototype.msgSuccess = function (msg) {
  this.$message({
    showClose: true,
    message: msg,
    type: "success"
  });
}
Vue.prototype.msgError = function (msg) {
  this.$message({
    showClose: true,
    message: msg,
    type: "error"
  });
}
Vue.prototype.msgWarning = function (msg) {
  this.$message({
    showClose: true,
    message: msg,
    type: "warn"
  });
}

Vue.prototype.msgInfo = function (msg) {
  this.$message.info(msg);
}

Vue.prototype.validForbid = function (value) {
  value = value.replace(/[`~!@#$%^&*()_\+=<>?:"{}|,./;'\\[\]·~！@#￥%……&*（）\+={}|《》？：“”【】、；‘’，。、]/g, '').replace(/\s/g, "");
  return value;
}

Vue.use(directive)
Vue.use(plugins)
Vue.use(VueMeta)
DictData.install()

/**
 * If you don't want to use mock-server
 * you want to use MockJs for mock api
 * you can execute: mockXHR()
 *
 * Currently MockJs will be used in the production environment,
 * please remove it before going online! ! !
 */

Vue.use(Element, {
  size: Cookies.get('size') || 'medium' // set element-ui default size
})

Vue.config.productionTip = false


//数组去重（从数组中移除所有相同元素）
Array.prototype.clearRepeat = function () {
  var newArr = [];
  var newObj = {};
  for (var i = 0; i < this.length; i++) {
    if (!newObj[this[i]]) {
      newArr.push(this[i]);
      newObj[this[i]] = 1;
    }
  }
  return newArr;
}
//判断数组中是否有相同元素
Array.prototype.indexOf = function (val) {
  for (var i = 0; i < this.length; i++) {
    if (this[i] == val)
      return i;
  }
  return -1;
};
//从数组中移除某个指定元素
Array.prototype.remove = function (val) {
  var index = this.indexOf(val);
  if (index > -1) {
    this.splice(index, 1);
  }
};

Vue.prototype.hasdictData  = function(){
  this.getDicts("plc_server_ip").then((response) => {
    return response.data[0].dictValue
    // console.log(response.data[0].dictValue);
  });
}

new Vue({
  el: '#app',
  router,
  store,
  render: h => h(App)
})
